package com.nuanshui.heatedloan.msgcenter.service;

import com.nuanshui.framework.utils.redis.JedisUtil;
import com.nuanshui.heatedloan.baseservice.service.CommonServiceImpl;
import com.nuanshui.heatedloan.constant.CacheConstants;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.msgcenter.facade.MsgRedDotStatusService;
import com.nuanshui.heatedloan.msgcenter.model.MsgBizType;
import com.nuanshui.heatedloan.msgcenter.model.MsgOperationType;
import com.nuanshui.heatedloan.msgcenter.model.MsgStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service(ServiceConstant.MSGREDDOTSTATUSSERVICE)
public class MsgRedDotStatusServiceImpl extends CommonServiceImpl implements MsgRedDotStatusService {

    @Autowired
    private JedisUtil jedisUtil;

    @Override
    public void mark(String userId, MsgBizType msgType, MsgOperationType op)
    {
        if(op== MsgOperationType.ADD || op== MsgOperationType.DELETE)
        {
            jedisUtil.lrem(CacheConstants.FEED_MSG_TYPE+userId,msgType.getTypeName());
        }

        if(op== MsgOperationType.ADD)
        {
            jedisUtil.lpush(CacheConstants.FEED_MSG_TYPE+userId,msgType.getTypeName());
        }
    }

    @Override
    public void mark(List<String> userList, MsgBizType msgType, MsgOperationType op)
    {
        for(String userId : userList){
            mark(userId,msgType,op);
        }
    }

    @Override
    public List<String> getStatus(String userId)
    {
        return jedisUtil.lrangeAllObj(CacheConstants.FEED_MSG_TYPE + userId, String.class);
    }

    public MsgStatus getMsgStatus(String userId)
    {
        MsgStatus msgStatus = new MsgStatus();
        List<String> types = getStatus(userId);
        if (types != null) {
            for (String type : types) {
                //系统消息
                if (MsgBizType.SYSTEM.toString().toLowerCase().equals(type)) {
                    msgStatus.setOfficial(true);
                }
                //审核消息
                if (MsgBizType.AUDIT.getTypeName().equals(type)) {
                    msgStatus.setReviewRedDot(true);
                }
                //还款消息
                if (MsgBizType.REPAYMENT.getTypeName().equals(type)) {
                    msgStatus.setRepaymentRedDot(true);
                }
            }
        }
        return msgStatus;
    }

}
